1   /*
2    * Copyright (C) 2008 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.collect;
18  
19  import com.google.common.annotations.GwtCompatible;
20  
21  /**
22   * Test cases for {@link Tables#transpose}.
23   * 
24   * @author Jared Levy
25   */
26  @GwtCompatible
27  public class TransposedTableTest extends AbstractTableTest {
28  
29    @Override protected Table<String, Integer, Character> create(
30        Object... data) {
31      Table<Integer, String, Character> original = HashBasedTable.create();
32      Table<String, Integer, Character> table = Tables.transpose(original);
33      table.clear();
34      populate(table, data);
35      return table;
36    }
37    
38    public void testTransposeTransposed() {
39      Table<Integer, String, Character> original = HashBasedTable.create();
40      assertSame(original, Tables.transpose(Tables.transpose(original)));
41    }
42    
43    public void testPutOriginalModifiesTranspose() {
44      Table<Integer, String, Character> original = HashBasedTable.create();
45      Table<String, Integer, Character> transpose = Tables.transpose(original);
46      original.put(1, "foo", 'a');
47      assertEquals((Character) 'a', transpose.get("foo", 1));
48    }
49    
50    public void testPutTransposeModifiesOriginal() {
51      Table<Integer, String, Character> original = HashBasedTable.create();
52      Table<String, Integer, Character> transpose = Tables.transpose(original);
53      transpose.put("foo", 1, 'a');
54      assertEquals((Character) 'a', original.get(1, "foo"));
55    }
56    
57    public void testTransposedViews() {
58      Table<Integer, String, Character> original = HashBasedTable.create();
59      Table<String, Integer, Character> transpose = Tables.transpose(original);
60      original.put(1, "foo", 'a');
61      assertSame(original.columnKeySet(), transpose.rowKeySet());
62      assertSame(original.rowKeySet(), transpose.columnKeySet());
63      assertSame(original.columnMap(), transpose.rowMap());
64      assertSame(original.rowMap(), transpose.columnMap());
65      assertSame(original.values(), transpose.values());    
66      assertEquals(original.row(1), transpose.column(1));
67      assertEquals(original.row(2), transpose.column(2));
68      assertEquals(original.column("foo"), transpose.row("foo"));
69      assertEquals(original.column("bar"), transpose.row("bar"));
70    }
71  }